<template>
	<div class="system-company-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="700px" :close-on-click-modal="false">
			<el-form ref="companyDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="所属平台">
							<el-select v-model="state.ruleForm.plat_id" filterable placeholder="请选择所属平台" clearable class="w100">
								<el-option v-for="(item,index) in state.platsData.palt" :key="item.id" :label="item.cname" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="父级">
							<el-select v-model="state.ruleForm.pid" filterable placeholder="请选择父级(默认没有父级)" clearable class="w100"  @change="selectpid">
								<el-option v-for="(item,index) in state.platsData.paltform" :key="item.id" :label="item.cname" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="渠道图片">
							<el-upload
								:show-file-list="false"
								:auto-upload="false"
								:accept="'image/*'"
								:on-change="onChange">
								<img v-if="state.ruleForm.img" :src="state.VITE_IMG_URL + state.ruleForm.img" class="my-avatar" />
								<div v-else class="my-upload-avatar">
									<SvgIcon name="ele-Plus" :size="20"/>
								</div>
							</el-upload>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="渠道名称">
							<el-input v-model="state.ruleForm.cname" placeholder="请输入渠道名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="调查ID">
							<el-input v-model="state.ruleForm.off_id" placeholder="请输入调查ID" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="渠道类型">
							<el-radio-group v-model="state.ruleForm.open_type">
								<!-- <el-radio :label="1">内链</el-radio> -->
								<el-radio :label="2">外链</el-radio>
								<el-radio :label="4">无关联</el-radio>
								<el-radio :label="3" v-if="!state.ruleForm.pid">列表</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="渠道星级">
							<el-rate v-model="state.ruleForm.star_num" allow-half />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" v-if="state.ruleForm.open_type != 3">
						<el-form-item label="渠道积分">
							<el-input v-model="state.ruleForm.pay" placeholder="请输入渠道积分" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态">
							<el-switch v-model="state.ruleForm.status" :active-value="1" :inactive-value="0" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number :max="99" :min="0" v-model="state.ruleForm.sort" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="国家">
							<el-input v-model="state.ruleForm.code" type="textarea" placeholder="请输入国家" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
	<!-- <CropperDialog ref="cropperDialogRef" @refresh_img="refresh_img"/> -->
</template>

<script setup name="systemCompanyDialog">
import { reactive, ref, defineAsyncComponent } from 'vue';



// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
import { platformApi } from '/@/api/platform/index';
import { commApi } from '/@/api/comm/index';


// 引入组件
// const CropperDialog = defineAsyncComponent(() => import('./cropper.vue'));

// 定义变量内容
// const cropperDialogRef = ref();

// 定义变量内容
const companyDialogFormRef = ref();
const state = reactive({
	VITE_IMG_URL:import.meta.env.VITE_IMG_URL,
	platsData:{
		company:[],
		palt:[],
		paltform:[]
	},
	ruleForm: {
		plat_id: '',
		cname: '',
		off_id: '',
		sort: 0,
		star_num: 5,
		open_type:2,
		pid:'',
		img: '',
		pay: '',
		status: 1, // 状态
		code:''
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});
// const refresh_img = (url) => {
// 	state.ruleForm.img = url;
// }
//选择父级
const selectpid = (val) => {
	if(val && state.ruleForm.open_type == 3){
		state.ruleForm.open_type = 2
	}
}
/**
 * 文件选择改变时触发
 */
const onChange = (file, fileListVal) => {
	if (file.status !== "ready") return;
	const formData = new FormData();
	formData.append('file', file.raw);
	commApi().upload(formData).then(res=>{
		state.ruleForm.img = res.data;
	})
	// onCropperDialogOpen(file.raw)
}
// // 打开裁剪弹窗
// const onCropperDialogOpen = (file) => {
// 	cropperDialogRef.value.openDialog(file);
// };
// 打开弹窗
const openDialog = (type, row) => {
	state.dialog.type = type
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.dialog.title = '修改渠道';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增渠道';
		state.dialog.submitTxt = '新 增';
		state.ruleForm = {
			plat_id: '',
			cname: '',
			off_id: '',
			sort: 0,
			star_num: 5,
			open_type:1,
			pid:'',
			img: '',
			pay: '',
			status: 1, // 状态
			code:''
		}
	}
	state.dialog.isShowDialog = true;
	getPlats()
};

const getPlats = () => {
	commApi().getPlats({paltform_type:3}).then(res=>{
		res.data.paltform.unshift({id: 0, cname: "无"})
		state.platsData = res.data
	})
}

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
	
};
// 提交
const onSubmit = () => {
	if (state.dialog.type === 'add') { 
		platformApi().add(state.ruleForm).then(async res=>{
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	}else{
		let data = JSON.parse(JSON.stringify(state.ruleForm))
		delete data.plat_name
		delete data.children
		platformApi().edit(data).then(async res=>{
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
<style lang="scss" scoped>
	.my-avatar{
		min-width: 120px;
		width: auto;
		height: 60px;
		border-radius: 8px;
		background: #000;
	}
	.my-upload-avatar{
		min-width: 120px;
		width: auto;
		height: 60px;
		border-radius: 8px;
	}
</style>
